package com.itinsys.enma.web;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itinsys.enma.domain.Career;
import com.itinsys.enma.domain.Subject;
import com.itinsys.enma.domain.response.EnmaSubjectResponse;
import com.itinsys.enma.domain.response.Response;
import com.itinsys.enma.domain.response.SubjectResponse;
import com.itinsys.enma.manager.CareerManager;
import com.itinsys.enma.manager.ResponseManager;
import com.itinsys.enma.manager.SubjectManager;
import com.itinsys.enma.service.SingletonService;

@Controller
@RequestMapping({ "/subject" })
public class SubjectFormController extends FormController {
	protected final Log logger = LogFactory.getLog("controller");
	
	@Autowired
	private SubjectManager subjectManager;
	
	@Autowired
	private CareerManager careerManager;

	@Autowired
	private ResponseManager responseManager;

	@Autowired
	private SingletonService singletonService;
	
	@RequestMapping(value = "/getAll", method = RequestMethod.GET)
	@ResponseBody
	public EnmaSubjectResponse getAllSubject() {
		return subjectManager.getAllSubjects();
	}

	@RequestMapping(value = { "/add" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public EnmaSubjectResponse addSubject(@RequestBody Map<String,String> subjectDTO,HttpServletResponse response) {
		final String description = (String) subjectDTO.get("description");
		final int careerYear = Integer.parseInt(subjectDTO.get("career_year"));
		final int careerId = Integer.parseInt(subjectDTO.get("idCareer"));
		
		Subject newSubject = new Subject();
		newSubject.setCareerYear(careerYear);
		newSubject.setDescription(description);
		
		Career aCareer = careerManager.getCareeById(careerId);
		EnmaSubjectResponse subResponse = subjectManager.addSubject(newSubject);
		EnmaSubjectResponse careerResp = careerManager.addSubject(aCareer, newSubject);
		if(careerResp.getResponseCode()==Response.OK){
			singletonService.collectionsInit();
		}else{
			responseManager.prepareResponse(subResponse, SubjectResponse.INSERT_ERROR);
		}
		return careerResp;
	
	}

	@RequestMapping(value = { "/edit" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public EnmaSubjectResponse editSubject(@RequestBody Map<String,String> SubjectDTO,HttpServletResponse response) {
		Subject subject = new Subject();
		subject.setId(Integer.parseInt(SubjectDTO.get("id")));
		subject.setDescription(SubjectDTO.get("description"));
		subject.setCareerYear(Integer.parseInt(SubjectDTO.get("subjectYear")));
		
		
		EnmaSubjectResponse subResponse = this.subjectManager.updateSubject(subject);
		EnmaSubjectResponse careerResp = careerManager.getAllSubjectsByCareer(Integer.parseInt(SubjectDTO.get("idCareer")));
		
		if(subResponse.getResponseCode()==Response.OK){
			singletonService.collectionsInit();
		}else{
			responseManager.prepareResponse(careerResp, SubjectResponse.INSERT_ERROR);
		}
		return careerResp;
	}
	

	@RequestMapping(value = { "/delete" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public EnmaSubjectResponse deleteSubject(@RequestBody Map<String,String> subjectDTO,HttpServletResponse response) {
		Subject subject = new Subject();
		subject.setId(Integer.parseInt(subjectDTO.get("id")));
		
		
		EnmaSubjectResponse subjectResponse = subjectManager.deleteSubject(subject);
		if(subjectResponse.getResponseCode()==Response.OK){
			singletonService.collectionsInit();
		}
		
		return subjectResponse;
	}
	


}
